package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-07-16
 * Time: 14:42
 */

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //此处精致创建对话，如果没找到，就认为用户是未登陆的
        HttpSession session = req.getSession(false);
        if (session == null) {
            //未登录
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("未登录");
        }
        //取出用户信息
        String username = (String) session.getAttribute("username");
        if (username == null) {
            //虽然有会话对象，但是里面没有必要的属性，也认为登录异常
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("当前用户未登录");
            return;
        }
        //如通过上述检查都ok，就生成一个动态界面
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("欢迎你" + username);
    }
}
